package com.qf.smsplatform.webmaster.security;

import com.qf.smsplatform.webmaster.mapper.UserMapper;
import com.qf.smsplatform.webmaster.pojo.MyAuthUser;
import com.qf.smsplatform.webmaster.pojo.TAdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class MyUserDetailsService implements UserDetailsService {

    private UserMapper userMapper;

    @Autowired
    public void setUserMapper(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TAdminUser user = userMapper.selectBycode(username);//从数据库中查询当前用户的信息

        MyAuthUser authUser = new MyAuthUser(username, user.getPassword(), new ArrayList<>());//将用户信息返回，主要是密码
        authUser.setId(user.getId());
        authUser.setType(user.getType());
        return authUser;
    }
}
